/**
 * Created by Administrator on 2016/7/26.
 */


var $ = (function($){
    //对象定义
    function Select(ID) {
        this.sel = $("#" + ID)
        this.isMulti = $("#" + ID).hasClass("multi")
        this.span = this.sel.children("span")
        this.inp = this.sel.children("input")
        this.option = this.sel.children("div")
        this.opts = this.option.children("div")
        this.timer;
    }
    //定义初始化方法
    Select.prototype.init = function () {
        var _this = this
        if(this.isMulti){
            this.inp.attr({placeholder:"可多选"})
        }else{
            if(!this.inp.attr("placeholder")){
                this.inp.attr({placeholder:"请选择"})
            }
        }
        //移入移除效果
        this.inp.mouseenter(function () {
            //节流阀(hover不到0.3不会触发)
            _this.timer = setTimeout(function () {
                _this.inp.css({"border-bottom-color":"#FFF"})
                _this.option.slideDown("100",function () {
                    _this.span.css({"background-position": "right bottom"})
                })
            },300)
        })
        this.sel.mouseleave(function () {
            clearTimeout(_this.timer)
            _this.option.slideUp("50",function () {
                _this.inp.css({"border-bottom-color":"lightblue"})
                _this.span.css({"background-position": "right top"})
            })
        })
        //点击选值
        this.opts.click(function () {
            if(_this.isMulti){
                $(this).toggleClass("blueBgc")
                $(this).attr("checked",$(this).hasClass("blueBgc"))
            }else{
                _this.inp.attr("value",$(this).attr("value"))
                _this.option.slideUp("50",function () {
                    _this.inp.css({"border-bottom":"1px solid lightblue"})
                    _this.span.css({"background-position": "right top"})
                })
            }
            var arr = _this.opts.filter(function(ind,val){
                return $(val).hasClass("blueBgc")
            })
            if(arr.length > 0){
                _this.inp.attr({placeholder:"已选择"})
            }else{
                _this.inp.attr({placeholder:"请选择"})
            }
        })
    }
    //挂载到jQuery上
    $.extend({
        Select:Select
    })
    return $
})($)

//给所有的插件初始化
$(".select").each(function (ind,val) {
    new $.Select($(val).attr("id")).init()
})